component output="false" extends="AbsService" accessors="true" {

	// Factories
	property name="SettingFactory" type="it.millemultimedia.easyBlog.factory.SettingFactory";
	// Delegate
	property name="PostDelegate" type="it.millemultimedia.easyBlog.delegate.PostDelegate";
	property name="BlogDelegate" type="it.millemultimedia.easyBlog.delegate.BlogDelegate";
	// Util
	property name="SessionMgr" type="it.millemultimedia.util.SessionMgr";
	property name="FeedManager" type="it.millemultimedia.util.FeedManager";

	public it.millemultimedia.easyBlog.service.PostService function init(){
		
		return this;
	}
	
	public it.millemultimedia.easyBlog.validator.PostValidator function getValidator(){
		
		return getPostDelegate().getValidator();
	}
	
	public it.millemultimedia.easyBlog.model.Post function new(){
		
		return getPostDelegate().createInstance();
	}
	
	public it.millemultimedia.easyBlog.model.Post function getPost( required Any Post ){
		
		
		var result = getPostDelegate().read( arguments.Post, false );
		
		return result;
	}
	
	public it.millemultimedia.easyBlog.model.Post[] function getPosts( required Any Post ){
	
		return getPostDelegate().list( post=arguments.post, adminMode=isAdminMode() );
		
	}
	
	public it.millemultimedia.easyBlog.model.Post[] function getPostsPaged( required Any Post, required Numeric start ){
	
		var list = "";
		
		// get list of post
		list =  getPostDelegate().list( arguments.post, false, arguments.start, 10, isAdminMode() );
		
		return list;
		
	}
	
	public Query function getPostCount(){
		
		return getPostDelegate().getPostCount( isAdminMode() );
		
	}
	
	public it.millemultimedia.easyBlog.model.Post[] function getArchive( required Any Post, required Numeric start ){
	
		var list = "";
		
		arguments.Post.setStatus( 1 );
	
		list =  getPostDelegate().list( arguments.post, false, arguments.start, 10, isAdminMode() );
		
		return list;
		
	}
	
	public boolean function postExists( required Any Post ){
		var result = getPostDelegate().list( arguments.Post );
		
		return arrayLen( result ) gt 0;
	}
	
	public void function savePost( required Any Post ){
	
		transaction{
		
			try{
			
				// convert title to urlsafe
				if( !len( arguments.Post.getUrlSafe() ) ){
				
					arguments.Post.setUrlSafe( convertToUrl( arguments.Post.getTitle() ) );
				
				}
				
				createFeed();
			
				if( arguments.Post.getIdPost() eq 0 ){
				
					this.createPost( arguments.Post );
				
				}else if( arguments.Post.getIdPost() gt 0 ){
				
					this.updatePost( arguments.Post );
				
				}else{
				
					throw( message="Post id property have undefined value." );
				
				}
			
			}catch( except Any ){
			
				transaction action="rollback";
				
				throw( except.message );
			
			}
		
		}
		
	}
	
	public void function createPost( required Any Post ){
	
		transaction{
		
			try{
			
				getPostDelegate().create( arguments.Post );
			
			}catch( except Any ){
			
				transaction action="rollback";
				
				throw( except.message );
			
			}
		
		}
		
	}
	
	public void function updatePost( required Any Post ){
	
		transaction{
		
			try{
			
				getPostDelegate().update( arguments.Post );
			
			}catch( except Any ){
			
				transaction action="rollback";
				
				throw( except.message );
			
			}
		
		}
		
	}
	
	public void function deletePost( required Any Post ){
	
		transaction{
		
			try{
			
				getPostDelegate().delete( arguments.Post );
				
			
			}catch( except Any ){
			
				transaction action="rollback";
				
				throw( except.message );
							
			}
			
		}
		
	}
	
	
	/* 
	********************************************************
															
					Support method							
															
	********************************************************
	 */
	
	private String function convertToUrl( required String value ){
		var result = "";
		var pattern = "[" & "'" & '"' & "##" & "/\\%&`@~!,:;=<>\+\*\?\[\]\^\$\(\)\{\}\|]";
		
		result = reReplace( arguments.value, pattern, "", "all" );
		result = replace( result, " ", "-", "all" );
		result = lCase( result );
	
		return result;
	}
	
	private boolean function isAdminMode(){
		
		return getSessionMgr().getValue( 'login' ).getStatus();
		
	}
	
	private void function createFeed(){
		var Post = this.new();
		var Blog = getBlogDelegate().createInstance();
		var list = [];
		
		Blog.setIdBlog( 'easyBlog' );
		
		Post.setStatus( 1 );
	
		list =  getPostDelegate().list( Post, false, 1, 10, false );
		
		getFeedManager().createFeed( getBlogDelegate().read( Blog ) ,list );
		
	}
	
}
